# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
# useful for handling different item types with a single interface

import re
from itemadapter import ItemAdapter
from scrapy.pipelines.files import FilesPipeline
from scrapy import Request

class NirsoftFilesPipeline(FilesPipeline):
    def get_media_requests(self, item, info):
        adapter = ItemAdapter(item)
        for file_url in adapter['file_urls']:
            # 确保使用完整URL
            yield Request(file_url, meta={'file_name': adapter.get('file_name')})

    def file_path(self, request, response=None, info=None, *, item=None):
        # 使用请求中的文件名元数据
        file_name = request.meta.get('file_name')
        if not file_name:
            file_name = request.url.split('/')[-1]
        # 确保文件名安全
        safe_name = re.sub(r'[\\/:*?"<>|]', '_', file_name)
        if not safe_name or all(c in r'_.-' for c in safe_name):
            safe_name = 'unknown_file.zip'
        return safe_name